﻿using GN.Pay.Application.Dto.Input;
using GN.Pay.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Application.Dto.Output
{
    /// <summary>
    /// 具有签名的响应输出
    /// </summary>
    public class ServiceSignMessageOutput : ServiceMessageErrorOutput
    {
        /// <summary>
        /// 应用Id
        /// </summary>
        [JsonProperty(Order = 3)]
        public string AppId { get; set; }

        /// <summary>
        /// 商户Id
        /// </summary>
        [JsonProperty(Order = 4)]
        public string MerchantId { get; set; }

        /// <summary>
        /// 随机字符
        /// </summary>
        [JsonProperty(Order = 5)]
        public string RandomStr { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        [JsonProperty(Order = 6)]
        public long TimeStamp { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        [JsonProperty(Order = 99)]
        public object Result { get; set; }
    }

    /// <summary>
    /// 服务消息输出
    /// </summary>
    public class ServiceMessageOutput : ServiceSignMessageOutput
    {
        private ServiceMessageOutput()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="output"></param>
        /// <param name="sign"></param>
        public ServiceMessageOutput(ServiceSignMessageOutput output, string sign)
        {
            Success = output.Success;
            Error = output.Error;
            AppId = output.AppId;
            MerchantId = output.MerchantId;
            RandomStr = output.RandomStr;
            TimeStamp = output.TimeStamp;
            Result = output.Result;
            Sign = sign;
        }

        /// <summary>
        /// 签名
        /// </summary>
        [JsonProperty(Order = 20)]
        public string Sign { get; set; }

        /// <summary>
        /// 创建异常输出
        /// </summary>
        /// <param name="code">代码</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ServiceMessageOutput CreateErrorOutput(int code, string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                message = "服务器发生异常";
            }
            return new ServiceMessageOutput()
            {
                Success = false,
                AppId = null,
                MerchantId = null,
                RandomStr = Guid.NewGuid().ToString("N"),
                Result = null,
                Sign = null,
                TimeStamp = DateTimeUtils.CurrentUtcTimeStampMilliseconds,
                Error = new ServiceErrorMessageOutput() { Code = code, Message = message }
            };
        }
    }

    /// <summary>
    /// 具有结果的输出
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    public class ServiceMessageResultOutput<TResult> : ServiceMessageErrorOutput
    {
        /// <summary>
        /// 结果
        /// </summary>
        [JsonProperty(Order = 99)]
        public TResult Result { get; set; }
    }    
}
